package com.atguigu.orderservice.service.impl;

import com.atguigu.commonutils.commonOrder.UCenterMemberOrder;
import com.atguigu.commonutils.commonOrder.UCourseOrder;
import com.atguigu.orderservice.client.UCourseClient;
import com.atguigu.orderservice.client.UMemberClient;
import com.atguigu.orderservice.entity.Order;
import com.atguigu.orderservice.mapper.OrderMapper;
import com.atguigu.orderservice.service.OrderService;
import com.atguigu.orderservice.util.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author haoyue1573
 * @since 2023-03-24
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

	@Resource
	private UCourseClient uCourseClient;
	@Resource
	private UMemberClient uMemberClient;
	@Resource
	private OrderService orderService;

	@Override
	public String saveOrder(String courseId,String userId) {
        //远程调用UCenterMemberController 服务
		UCenterMemberOrder memberOrder = uMemberClient.getMemberById(userId);
		//远程调用添加课程的服务
		UCourseOrder courseOrder = uCourseClient.getCourseOrderById(courseId);

		//创建订单
		Order order = new Order();
		order.setOrderNo(OrderNoUtil.getOrderNo());
		order.setCourseId(courseId);
		order.setCourseTitle(courseOrder.getTitle());
		order.setCourseCover(courseOrder.getCover());
		order.setTeacherName("test");
		order.setTotalFee(courseOrder.getPrice());
		order.setMemberId(memberOrder.getId());
		order.setMobile(memberOrder.getMobile());
		order.setNickname(memberOrder.getNickname());
		order.setStatus(0);
		order.setPayType(1);
		//保存订单
		orderService.save(order);
		return order.getOrderNo();
	}
}
